<template>
	<div style="display: contents;">
		<el-button
			icon="el-icon-download"
			size="small"
			type="success"
			@click="pull"
			circle
			style="margin-left: 10px;"
		></el-button>
	</div>
</template>

<script>
	import when from "when";
	const { exec } = require("child_process");
	const fixPath = require("fix-path");
	export default{
		name:'pull',
		data(){
			return {
				
			}
		},
		methods: {
			pull() {
				
				if (this.$store.state.Config.config.shellDeploy) {
					this.pullHexoWithShell();
				} else {
					this.pullHexoWithGit();
				}
			},
		
			pullHexoWithShell() {
				const me = this;
				me.deployConsoleText = "发布中...\n";
				me.deployConsoleResize();
				me.showConsoleLoading = true;
				this.doDeployHexoWithShell().then(
					function() {
						// 执行成功
						me.deployConsoleAppendText("\n执行成功");
						me.$message({
							showClose: true,
							message: "发布成功",
							duration: 2000,
							type: "success",
							onClose: function() {
								me.showConsoleLoading = false;
							}
						});
					},
					function(err) {
						// 执行失败
						me.deployConsoleAppendText("\n执行失败");
						me.$message({
							showClose: true,
							message: "发布失败",
							duration: 2000,
							type: "error",
							onClose: function() {
								me.showConsoleLoading = false;
							}
						});
					},
					function(data) {
						// 执行中
						me.deployConsoleAppendText(data);
					}
				)
			},
			doDeployHexoWithShell() {
				var deferred = when.defer();
				fixPath();
				const workProcess = exec("hexo generate -d", {
					cwd: this.$store.state.Config.config.path,
					maxBuffer: 5000 * 1024 // 默认 200 * 1024
				});
			
				// 打印正常的后台可执行程序输出
				workProcess.stdout.on("data", function(data) {
					deferred.notify(data);
				});
			
				// 打印错误的后台可执行程序输出
				workProcess.stderr.on("data", function(data) {
					deferred.notify(data);
				});
			
				// 退出之后的输出
				workProcess.on("close", function(code, signal) {
					if (code === 0) {
						deferred.resolve(0);
					} else {
						deferred.reject(code, signal);
					}
				});
			
				return deferred.promise;
			},
			
			async pullHexoWithGit() {
				let success = true;
				let loading = this.$loading({text:'Pulling ...'})
				try {
						await this.git().pull();
						
				} catch (e) {
						success = false;

				} finally {
						loading.close();
						this.$message({
							showClose: true,
							message: success ? "更新成功" : "更新失败",
							duration: 2000,
							type: success ? "success" : "error"
						});
				}
			},
			async commit(branch, msg) {
				let success = true;
				let loading = this.$loading({
					lock: true,
					text: "发布中请稍后...",
					spinner: "el-icon-loading",
					background: "rgba(0, 0, 0, 0.7)"
				});

				try {
					await this.git().add("./*");
					await this.git().commit(msg);
					await this.git().push("origin", branch);
				} catch (e) {
					success = false;
				} finally {
					loading.close();
					this.$message({
					showClose: true,
					message: success ? "发布成功" : "发布失败",
					duration: 2000,
					type: success ? "success" : "error"
					});
				}
			},
		
			git() {
				let workingDir = this.workingDir();
				return require("simple-git/promise")(workingDir);
			},
	
			workingDir() {
				let config = this.$store.state.Config.config;
				return config.path;
			},
			dateFormat(fmt, date) {
				var o = {
					"M+": date.getMonth() + 1,
					"d+": date.getDate(),
					"h+": date.getHours(),
					"m+": date.getMinutes(),
					"s+": date.getSeconds(),
					"q+": Math.floor((date.getMonth() + 3) / 3),
					S: date.getMilliseconds()
				};
				if (/(y+)/.test(fmt)) {
					fmt = fmt.replace(
						RegExp.$1,
						(date.getFullYear() + "").substr(4 - RegExp.$1.length)
					);
				}
				for (var k in o) {
					if (new RegExp("(" + k + ")").test(fmt)) {
					fmt = fmt.replace(
						RegExp.$1,
						RegExp.$1.length === 1
						? o[k]
						: ("00" + o[k]).substr(("" + o[k]).length)
					);
					}
				}
				return fmt;
			},
	
			deployConsoleAppendText(text) {
				this.deployConsoleText += text;
				if (
					this.$refs.consoleScrollEnd &&
					this.$refs.consoleScrollEnd.scrollIntoView
				) {
					this.$refs.consoleScrollEnd.scrollIntoView(); // 滚动到最底部
				}
			},
		
			deployConsoleResize() {
				this.deployConsoleWidth = document.documentElement.clientWidth + "px";
				this.deployConsoleHeight = document.documentElement.clientHeight + "px";
			}
		}
	}
</script>

<style>
</style>
